<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
		$this->load->library('tank_auth');
		$this->lang->load('tank_auth');
        if(!$this->tank_auth->is_logged_in())
            redirect('administrator/auth');
        $this->load->model('menus','men');
        $this->load->model('tank_auth/users','use');
    }

    /**
     * Default function
     */
    public function index($act=false)
    {
        $post= $this->input->post(null);
        $this->_layout['action'] = $this->uri->segment(4);
        $this->_layout['id'] = $this->uri->segment(5);
        
        $users = $this->use->get_user();
        $this->assign('users',$users);
        $this->assign('action','');
        
        if($act)
        {   if($act!="detail"){
            if($act=="save"){
                if($post['name'] ==""){
                            _set_error_message('....');
                            redirect('administrator/user/index');
                        }
                switch ($post['action']) {
                    case 'create':
                        $rs = $this->tank_auth->create_user($post['username'],$post['email'],$post['password'],false);
                        if(!empty($rs))
                        {
                            $profile = array('user_id'=>$rs['user_id'],'country'=>$post['country'],'website'=>$post['website'],'name'=>$post['name']
                        ,'birthdate'=>strtotime($post['birthdate']),'gender'=>$post['gender'],'address'=>$post['address']
                        ,'favorite'=>$post['favorite'],'job'=>$post['job'],'company'=>$post['company'],'more'=>$post['more']);
                            $rs = $this->use->create_profile($profile);
                            _set_success_message('Save successful');
                        }
                        else
                            _set_error_message('....');
                        redirect('administrator/user/index');
                        break;
                    case 'edit':
                        $profile = array('country'=>$post['country'],'website'=>$post['website'],'name'=>$post['name']
                        ,'birthdate'=>strtotime($post['birthdate']),'gender'=>$post['gender'],'address'=>$post['address']
                        ,'favorite'=>$post['favorite'],'job'=>$post['job'],'company'=>$post['company'],'more'=>$post['more']);
                        $rs = $this->use->update_profile($profile,array('user_id'=>$post['user_id']));
                        if($rs)
                        _set_success_message('Save successful');
                        else
                            _set_error_message('....');
                        redirect('administrator/user/index');
                    break;
                }
                
            }
            elseif($act=="delete"){
                if(!is_numeric($this->_layout['id']) || !$this->_layout['id']){
                    _set_error_message('Cant delete this user');
                    redirect('administrator/user/index');
                }
                $rs = $this->use->delete_user($this->_layout['id']);
                if($rs){
                    _set_success_message('Deleted...');
                }else
                    _set_error_message('Cant delete this user');
                redirect('administrator/user/index');
            }elseif($act=="banned"){
                if(!is_numeric($this->_layout['id']) || !$this->_layout['id']){
                    _set_error_message('Cant banned this user');
                    redirect('administrator/user/index');
                }
                $this->use->ban_user($this->_layout['id']);
                redirect('administrator/user/index');
            }elseif($act=="unbanned"){
                if(!is_numeric($this->_layout['id']) || !$this->_layout['id']){
                    _set_error_message('Cant unbanned this user');
                    redirect('administrator/user/index');
                }
                $this->use->unban_user($this->_layout['id']);
                redirect('administrator/user/index');
            }elseif($act=="create"){
                //@TODO
            }
            elseif($act=="edit"){
                $this->_layout['profile'] = $this->use->get_profile($this->uri->segment(5));
                $form   = $this->load->view('layouts/form/user',$this->_layout,true);
            }
            elseif($act=="customer"){
                echo 1;
            }
            else show_404();
            }
            if($act=="detail"){
                $this->_layout['profile'] = $this->use->get_profile($this->uri->segment(5));
                $form   = $this->load->view('layouts/form/user-profiles',$this->_layout,true);
            }elseif($act=="delete"){
                if(!is_numeric($this->_layout['id']) || !$this->_layout['id']){
                    _set_error_message('Cant delete this user');
                    redirect('administrator/user/index');
                }
                $rs = $this->use->delete_user($this->_layout['id']);
                if($rs){
                    _set_success_message('Deleted...');
                }else
                    _set_error_message('Cant delete this user');
                redirect('administrator/user/index');
            }elseif($act=="banned"){
                if(!is_numeric($this->_layout['id']) || !$this->_layout['id']){
                    _set_error_message('Cant banned this user');
                    redirect('administrator/user/index');
                }
                $this->use->ban_user($this->_layout['id']);
                redirect('administrator/user/index');
            }elseif($act=="unbanned"){
                if(!is_numeric($this->_layout['id']) || !$this->_layout['id']){
                    _set_error_message('Cant unbanned this user');
                    redirect('administrator/user/index');
                }
                $this->use->unban_user($this->_layout['id']);
                redirect('administrator/user/index');
            }else{
                $form   = $this->load->view('layouts/form/user',$this->_layout,true);
            }
            $this->assign('action',$form);
        }
    }
    
    
}